let socket={};
let client=null;

socket.connect=function init(url,
                             user,
                             contacts,
                             message,
                             beSingleCall,
                             beMoreCall,
                             getRoomcall,
                             createdCall,
                             joinCall,
                             beRefuseCall,
                             someBodyOutCall,
                             userBusyCall,
                             someBodyCall) {
    if(client==null){
        client=io.connect(url,{
            transports: ['websocket'],
            query: {
                auth_sign: 'abcdefg'
            }
        });
        client.on('connectSuccess',function (data) {
            let vo={};
            vo['id']=user['id'];
            vo['info']=user;
            client.emit('online',vo);
        });
        client.on('getContactsRev',function (data) {
            console.log(data);
            contacts(data);
        });
        client.on('messageRev',function (data) {
            message(data);
        });

        client.on('beSingleCall',function (data) {
            beSingleCall(data);
        });
        client.on('beMoreCall',function (data) {
            console.log('有人呼叫',data);
            beMoreCall(data);
        });
        client.on('roompermissionRev',function (data) {
            getRoomcall(data);
        });
        client.on('created',function () {
            createdCall();
        });
        client.on('join',function (roomid,uid) {
            joinCall(uid);
        });
        client.on('beRefuseCall',function (data) {
            beRefuseCall(data);
        });
        client.on('outRoom',function (roomid,uid) {
            someBodyOutCall(uid);
        });
        client.on('userBusy',function (uid) {
            userBusyCall(uid);
        });
        client.on('someBodyCall',function (data) {
            someBodyCall(data);
        });

    }
};

socket.getRoom=function room(id) {
    if(client!=null){
        let vo={};
        vo['from']=id;
        client.emit('roompermission',vo);
    }
};

socket.intoRoom=function into(room,id) {
    let intoRoom={};
    intoRoom['room']=room;
    intoRoom['userId']=id;
    intoRoom['type']=0;
    if(client!=null){
        client.emit('into',intoRoom);
    }
};
socket.intoMoreRoom=function into(room,id) {
    let intoRoom={};
    intoRoom['room']=room;
    intoRoom['userId']=id;
    intoRoom['type']=1;
    if(client!=null){
        client.emit('into',intoRoom);
    }
};

socket.singleCall=function singleCall(room,from,to,beRefuse) {
    console.log("发起呼叫");
    let call={};
    call['room']=room;
    call['from']=from;
    call['to']=to;
    if(client!=null){
        client.emit('singleCall',call);
    }
};
socket.moreCall=function moreCall(room,from,tos) {
    console.log("发起呼叫");
    let call={};
    call['room']=room;
    call['from']=from;
    call['to']=tos;
    if(client!=null){
        client.emit('moreCall',call);
    }
};

socket.sendSDP=function sendSDP(sdp,from,to) {
    let call={};
    call['type']=sdp.type;
    call['sdp']=sdp.sdp;
    call['from']=from;
    call['to']=to;
    if(client!=null){
        client.emit('message',call);
    }
};

socket.sendIceData=function sendIceData(ice,from,to) {
    let call={};
    call['type']='candidate';
    call['label']=ice.sdpMLineIndex;
    call['id']=ice.sdpMid;
    call['candidate']=ice.candidate;
    call['from']=from;
    call['to']=to;
    if(client!=null){
        client.emit('message',call);
    }
};

socket.outRoom=function out(room,uid){
  if(client!=null){
      let call={};
      call['room']=room;
      call['userId']=uid;
      client.emit('out',call);
  }
};
socket.refuseCall=function refuse(room,from,to){
    if(client!=null){
        let call={};
        call['room']=room;
        call['from']=from;
        call['to']=to;
        client.emit('refuseCall',call);
    }
};

socket.close=function close() {
    if(client!=null){
        client.close();
        client=null;
    }
};
Array.prototype.remove = function(val) {
    let index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};

